extends layout

block content
  div#configForm.form-horizontal.container(style='padding: 10px;')
    fieldset
        legend
          h5 接口参数：
        div
          .control-group.span5
            label.control-label 路径:
            .controls
              input#path.required(type='text', name='path', value='', placeholder='必填')
          .control-group.span5
            label.control-label 方式:
            .controls
              select#method.required(name='method')
                option(value='GET')='GET'
                option(value='POST')='POST'
          .control-group.span10
            label.control-label data:
            .controls
              textarea#data(name='data', value='', style="width:100%;height:100px;")
          .control-group.span10
            label.control-label
            .controls
              label.checkbox.inline
                input#jsAction(type='checkbox')
                span(title='每次循环都由前端发起一次请求') 前端交互
              label.checkbox.inline
                input#remoteAction(type='checkbox')
                span(title='根据请求参数到远程服务器获取信息') 远程交互
          .control-group.span10
            label.control-label 发送次数:
            .controls
              input#times.span1.required(type='text', name='times', value='1', placeholder='必填')
              input#performence_btn.btn.btn-primary(style='margin-left:5px;', type='submit', value='发送请求',onClick='doPerformence();')
          .control-group.span10
            label.control-label Console:
            .controls
              textarea(id='content',name='content', style="width:100%;height:400px;")
  p.pull-right 本系统采用Node.js编写
  script(type="text/javascript").
    //执行发送请求
    function doSend() {
      $.get("/api", {
        path:$("#path").val(),
        method:$("#method").val(),
        data:$("#data").val(),
        times:$("#times").val(),
        remoteAction:$("#remoteAction").is(":checked"),
        t : new Date().getTime()
      }, function(data, status) {
        $("#content").val(new Date().toLocaleString() + ":" + JSON.stringify(data) + "\r\n"+ $("#content").val());
        count++;
        if(!$("#jsAction").is(":checked")||count>=$("#times").val()){
          var endTime=new Date();
          var costTime=endTime-startTime;
          var content="============================================\r\n";
          content+="开始时间:"+startTime.toLocaleString()+"."+startTime.getMilliseconds()+"\r\n";
          content+="结束时间:"+endTime.toLocaleString()+"."+endTime.getMilliseconds()+"" + "\r\n"
          content+="次数:"+$("#times").val()+"次\r\n";
          content+="耗时:"+costTime+"ms\r\n";
          content+="速度:"+Math.round($("#times").val()*1000/costTime)+"次/秒\r\n";
          $("#content").val(content+ $("#content").val());
        }
      });
    }
    //性能测试,循环执行发送请求
    var count=0;
    var startTime=new Date();
    function doPerformence(){
      if(!doCheck()){
        return;
      }
      count=0;
      startTime=new Date();
      if($("#jsAction").is(":checked")){
        for(var i=0;i<$("#times").val();i++){
          doSend();
        }
      }else{
        doSend();
      }
      //有效性检查
      function doCheck(){
        if($("#path").val()==""){
          alert("路径不能为空");
          return false;
        }
        return true;
      }
    }